package com.groupbuying.customer.controller;

import com.groupbuying.customer.domain.CustomerUshopDO;
import com.groupbuying.customer.service.CustomerService;
import com.groupbuying.customer.service.CustomerUshopService;
import com.groupbuying.customer.service.SmsService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author gxy
 * @Description 发送短信工具类
 * @Date 2019/5/27 10:05
 **/
@RestController
@RequestMapping("/sms")
public class SmsController {
    private static final Logger logger = LoggerFactory.getLogger(SmsController.class);

    @Autowired
    private SmsService smsService;

    @Autowired
    private CustomerUshopService customerUshopService;


   /**
    * @api {post} /sms/sendSms 发送短信接口 ，mobile和content不可为空，mobile为发送手机号，content为发送内容
    * @apiExample
    * author mayunfei
    * tar 用于:TODO 未知调用方
    * date 2019/10/31
    * @apiGroup bus-customer
    * @apiVersion 1.0.0
    * @apiHeader {String} Authorization 用户授权token
    * @apiParam {Map} mobile:发生手机号
    * @apiParam {Map} content:发送内容
    * @apiSuccess {boolean} flag
    */
    @ApiOperation(value = "发送短信接口", notes = "mobile和content不可为空，mobile为发送手机号，content为发送内容")
    @GetMapping("/sendSms")
    public boolean sendSms(@RequestParam Map<String, String> params){
        return smsService.sendSms(params);
    }

    /**
     * @api {post} /sms/sendSmsWithCustomerId 发送短信接口
     * @apiExample
     * author mayunfei
     * tar 用于:TODO 未知调用方
     * date 2019/10/31
     * @apiGroup bus-customer
     * @apiVersion 1.0.0
     * @apiHeader {String} Authorization 用户授权token
     * @apiParam {String} customerId:会员id
     * @apiParam {String} msg:短信内容
     * @apiSuccess {void} null
     */
    @ApiOperation(value = "发送短信接口", notes = "发送短信接口")
    @GetMapping("/sendSmsWithCustomerId")
    public void sendSms(String customerId, String msg){
        CustomerUshopDO customerUshopDO = customerUshopService.get(customerId);
        if(null != customerUshopDO){
            Map contentMap = new HashMap();
            contentMap.put("mobile",customerUshopDO.getMobile());
            contentMap.put("content", msg);
            logger.error("发送短信，内容" + msg + ",手机号：" + customerUshopDO.getMobile());
            smsService.sendSms(contentMap);
        }
    }
}
